<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="700" destroy-on-close @closed="$emit('closed')" align-center>
		<el-container>
			<el-header>
                <div class="left-panel">
                    <el-button type="primary" @click="back">确定</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="add">添加不良品原因</el-button>
                </div>
                <div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keywords" placeholder="请输入关键词" clearable @change="upsearch"></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
					</div>
				</div>
            </el-header>
			<el-main class="nopadding">
				<scTable ref="table" row-key="id" @select-all="selectAll" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border height="550">
					<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                    <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
					<el-table-column label="编号" prop="rejects_code"></el-table-column>
					<el-table-column label="名称" prop="rejects_name"></el-table-column>
				</scTable>
			</el-main>
	    </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
	</el-dialog>
</template>

<script>
import saveAdd from './save.vue'
export default {
  components: { saveAdd },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择不良品',
				edit: '编辑不良品',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			selection: [],
			apiObj: this.$API.craft.rejects.list,
			search: {
				keywords: null,
			},
            dialog: {
                save: false
			},
		}
	},
	mounted() {
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// 表单注入数据
		setData(data) {
            this.index = data
		},
        // 选择事件
        selectionChange(selection) {
            if(this.mode == 'bom'){
                if(selection.length > 1){
                    const del_row = selection.shift()
                    this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
                }
            }
			this.selection = selection;
		},
        selectAll() {
            if(this.mode == 'bom'){
                this.$message.error("只能单选！")
			    this.$refs.table.clearSelection()
            }
		},
        // 选中返回
        back(){
            this.$emit('success', this.selection,this.mode,this.index)
            this.visible = false;
        },
        //新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        // 更新新增
        handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        // 重置
        clearSearch(){
            this.search.text_select = null
        },
	}
}
</script>

<style></style>
